package com.daxiong.hfs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.daxiong.hfs.bean.MainBean;
import com.daxiong.hfs.bean.ProductAdapterBean;
import com.daxiong.hfs.bean.ProductBean;
import com.daxiong.hfs.constant.Constant;
import com.daxiong.hfs.util.CommonUtil;
import com.google.analytics.tracking.android.EasyTracker;

public class ProductResultActivity extends Activity {

	private MainBean mainBean;

	private ListView listView;

	private void initialize() {
		mainBean = CommonUtil.getMainBeanFormIntent(getIntent());
	}

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.product_result_layout);

		initialize();

		setupViews();
	}

	@Override
	public void onStart() {
		super.onStart();

		EasyTracker.getInstance(this).activityStart(this);
	}

	@Override
	public void onStop() {
		super.onStop();

		EasyTracker.getInstance(this).activityStop(this);
	}

	private void setupViews() {
		listView = (ListView) findViewById(R.id.productListView);

		ProductAdapterBean productAdapterBean = //
		new ProductAdapterBean(this, mainBean.getProductResultList());

		listView.setAdapter(productAdapterBean);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick( //
					final AdapterView<?> parent, //
					final View view, //
					final int position, //
					final long id //
			) {
				final ProductBean productBean = //
				(ProductBean) listView.getItemAtPosition(position);

				mainBean.setProductSelect(productBean);

				final Context context = ProductResultActivity.this;

				final Class<?> clazz = ProductDetailActivity.class;

				final Intent intent = new Intent(context, clazz);

				final Bundle bundle = new Bundle();

				bundle.putSerializable(Constant.MAIN_BEAN_KEY, mainBean);

				intent.putExtras(bundle);

				startActivity(intent);
			}
		});
	}
}
